package org.example.adb;

import java.io.BufferedReader;
import java.io.InputStreamReader;

//3.将文件推送到设备
public class AdbPushExample {
    public static void main(String[] args) {
        String localPath = "C:/Users/YourName/Desktop/example.txt";
        String remotePath = "/sdcard/Download/example.txt";

        boolean success = pushFileToDevice(localPath, remotePath);
        if (success) {
            System.out.println("File pushed successfully.");
        } else {
            System.out.println("Failed to push file.");
        }
    }

    public static boolean pushFileToDevice(String localPath, String remotePath) {
        try {
            ProcessBuilder builder = new ProcessBuilder("adb", "push", localPath, remotePath);
            Process process = builder.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder output = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }

            process.waitFor();

            if (process.exitValue() == 0) {
                return true;
            } else {
                System.err.println("Error pushing file: " + output.toString());
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
